

package BitCreeekClient;

/**
 * Created by Ivan Litsvinenka e Emanuele Ricci
 * Date: 3-ago-2009
 * Time: 16.46.25
 */


public class PeerP2PClientThread implements Runnable {
    int port;
    String torrentName;

    public PeerP2PClientThread ( int port, String torrentName ) {
        this.port = port;
        this.torrentName = torrentName;
    }

    public void run () {


        try {

            while ( true ) {

                if ( Client.downloads.get ( torrentName ).getProgress () == 100 ) return;

                if ( Client.downloads.get ( torrentName ).getSwarm ().size () < 1 ) {
                    Thread.sleep ( 1000 );
                }

                
                
                int rarestPart;
                
                do {
                	Client.downloads.get ( torrentName ).analizeAvaibility ();
                	rarestPart = Client.downloads.get ( torrentName ).getRarestPart ();
                	if ( rarestPart < 0 ) {
                		Thread.sleep(1000);
                	}
                } while ( rarestPart < 0 );
                
                Client.downloads.get ( torrentName ).parts[ rarestPart ].setDownloading ();
                PeerP2PClientWorker p2pCW = new PeerP2PClientWorker ( torrentName, rarestPart );
                try { p2pCW.run (); }
				catch ( NullPointerException e ) {
					
				}
            }
        } catch ( Exception e ) {
            e.printStackTrace ();
        }

    }
}

